{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\"> Prime Numbers using Python </h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What is a prime number?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Has to be a natural number (postive integer) \n",
    "2. Divisible by exactly 2 natural numbers.\n",
    "3. 1 is not a prime number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Task: Write a program to make a list of all prime numbers less than or equal to 20. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Approach 1: For Loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize a list\n",
    "primes = []\n",
    "for possiblePrime in range(2, 21):\n",
    "    \n",
    "    # Assume number is prime until shown it is not. \n",
    "    isPrime = True\n",
    "    for num in range(2, possiblePrime):\n",
    "        if possiblePrime % num == 0:\n",
    "            isPrime = False\n",
    "      \n",
    "    if isPrime:\n",
    "        primes.append(possiblePrime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Approach 2: For Loop with Break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Only difference is a break statement. Basically once you know that a number isn't prime why should you keep on testing it in the inner loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Initialize a list\n",
    "primes = []\n",
    "for possiblePrime in range(2, 21):\n",
    "    \n",
    "    # Assume number is prime until shown it is not. \n",
    "    isPrime = True\n",
    "    for num in range(2, possiblePrime):\n",
    "        if possiblePrime % num == 0:\n",
    "            isPrime = False\n",
    "            break\n",
    "      \n",
    "    if isPrime:\n",
    "        primes.append(possiblePrime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Approach 1 vs 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Approach 2 is more efficient. Say you were trying to check whether 15 is a prime number. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "possiblePrime = 15\n",
    "\n",
    "# Assume number is prime until shown it is not. \n",
    "isPrime = True\n",
    "for num in range(2, possiblePrime):\n",
    "    print(num)\n",
    "    if possiblePrime % num == 0:\n",
    "        isPrime = False\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "\n",
    "possiblePrime = 15\n",
    "\n",
    "# Assume number is prime until shown it is not. \n",
    "isPrime = True\n",
    "for num in range(2, possiblePrime):\n",
    "    print(num)\n",
    "    if possiblePrime % num == 0:\n",
    "        isPrime = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Approach 3: For Loop, Break, and Square Root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Initialize a list\n",
    "primes = []\n",
    "for possiblePrime in range(2, 21):\n",
    "    \n",
    "    # Assume number is prime until shown it is not. \n",
    "    isPrime = True\n",
    "    for num in range(2, int(possiblePrime ** 0.5) + 1):\n",
    "        if possiblePrime % num == 0:\n",
    "            isPrime = False\n",
    "            break\n",
    "      \n",
    "    if isPrime:\n",
    "        primes.append(possiblePrime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Approach 4: Sieve of Eratosthenes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "I will add in the future if necessary. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
